//
//  CountryViewController.m
//  Lovelife
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "CountryViewController.h"
#import "AreaHeadReusableView.h"
#import "AFNetWorkingYU.h"
#import "CityViewController.h"
#import "AreaCell.h"
#import "RecommendTripViewController.h"
#import "SightseeingWebView.h"
#import "OtherCitiesViewController.h"
#import "CountryModel.h"
#import "CityModel.h"
#import "UIImageView+WebCache.h"
#import "NightManager.h"

@interface CountryViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,AreaHeadReusableViewDelegate>
@property (nonatomic, retain) UIActivityIndicatorView *activityIndicatorView;
@property (nonatomic, retain) AreaHeadReusableView *headView;
@property (nonatomic, retain) NSMutableArray *arrayOfCity;
@property (nonatomic, retain) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) CountryModel *country;
@property (nonatomic, retain) BaseButton *moreCityButton;
@end

@implementation CountryViewController
- (void)dealloc
{
    [_countryId release];
    [_activityIndicatorView release];
    [_headView release];
    [_arrayOfCity release];
    [_flowLayout release];
    [_collectionView release];
    [_country release];
    [_moreCityButton release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arrayOfCity = [NSMutableArray array];

    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 旋转进度轮
    self.activityIndicatorView = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 50, 150, 100, 100)] ;
    [self.activityIndicatorView setActivityIndicatorViewStyle: UIActivityIndicatorViewStyleWhiteLarge];
    self.activityIndicatorView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.activityIndicatorView];
    [_activityIndicatorView release];
    [self getData];
}
- (void)createView
{
    // 创建item
    self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.flowLayout.itemSize = CGSizeMake(130, 180);
    self.flowLayout.scrollDirection = 0;
    self.flowLayout.sectionInset = UIEdgeInsetsMake(40, 20, 20, 20);
    self.flowLayout.minimumInteritemSpacing = 20;
    self.flowLayout.minimumLineSpacing = 20;
    self.flowLayout.headerReferenceSize = CGSizeMake(20, 250);
    self.flowLayout.footerReferenceSize = CGSizeMake(20, 70);
    [_flowLayout release];

    // 创建瀑布流
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:self.flowLayout];
    [NightManager setBackgroundColorWithCollectionView:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    [_collectionView release];

    // 注册
    [self.collectionView registerClass:[AreaCell class] forCellWithReuseIdentifier:@"reuse"];
    [self.collectionView registerClass:[AreaHeadReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"foot"];
    
    // 创建更多城市按钮
    self.moreCityButton = [BaseButton buttonWithType:UIButtonTypeCustom];
    self.moreCityButton.frame = CGRectMake(self.view.frame.size.width / 2 - 80, 10 , 160, 50);
    self.moreCityButton.backgroundColor = [UIColor blackColor];
    [self.moreCityButton setTitle:@"更多城市" forState:UIControlStateNormal];
    [self.moreCityButton addTarget:self action:@selector(selectMoreCity) forControlEvents:UIControlEventTouchUpInside];
}
// 查看更多城市
- (void)selectMoreCity
{
    OtherCitiesViewController *otherCityVC = [[OtherCitiesViewController alloc]init];
    otherCityVC.countryId = self.countryId;
    [self.navigationController pushViewController:otherCityVC animated:YES];
    [otherCityVC release];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arrayOfCity.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AreaCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    CityModel *city = self.arrayOfCity[indexPath.row];
    [cell.photo sd_setImageWithURL:[NSURL URLWithString:city.photo] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
    cell.nameLabel.text = [NSString stringWithFormat:@"%@\n%@",city.cnname,city.enname];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CityViewController *cityVC = [[CityViewController alloc]init];
    CityModel *city = self.arrayOfCity[indexPath.row];
    cityVC.cityId = city.cityId;
    [self.navigationController pushViewController:cityVC animated:YES];
    [cityVC release];
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        self.headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"head" forIndexPath:indexPath];
        self.headView.delegate = self;
        self.headView.photoArr = self.country.photos;
        self.headView.ifHaveGuide = !self.country.has_guide;
        if (self.country.overview_url.length == 0) {
            self.headView.ifHaveInformation = YES;
        }
        self.headView.titleLabel.text = [NSString stringWithFormat:@"%@城市",self.country.cnname];
        self.headView.nameLable.text = [NSString stringWithFormat:@"%@\n%@",self.country.cnname,self.country.enname];
        return self.headView;
    }else{
        UICollectionReusableView *foot = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"foot" forIndexPath:indexPath];
        [foot addSubview:self.moreCityButton];
        return foot;
    }
}
- (void)push:(NSInteger)tag
{
    if (tag == 102) {
        RecommendTripViewController *recommendTripVC = [[RecommendTripViewController alloc]init];
        recommendTripVC.areaType = YES;
        recommendTripVC.areaId = self.countryId;
        [self.navigationController pushViewController:recommendTripVC animated:YES];
        [recommendTripVC release];
    }else{
        SightseeingWebView *webVC = [[SightseeingWebView alloc]init];
        webVC.url = self.country.overview_url;
        webVC.titleName = @"实用信息";
        [self.navigationController pushViewController:webVC animated:YES];
        [webVC release];
    }
}
- (void)getData
{
    [self.activityIndicatorView startAnimating];
    [AFNetWorkingYU netWorkingHanderGetGataWithUrl:[NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/country_detail?app_installtime=1429487962&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&country_id=%@&lat=38.88299841813002&lon=121.5398914286667&page=1&track_app_version=6.2",self.countryId] resultBlock:^(id result) {
        NSDictionary *dic = result[@"data"];
        
        self.country = [[CountryModel alloc]init];
        [self.country setValuesForKeysWithDictionary:dic];
        self.country.photos = [NSMutableArray array];
        // 照片数组
        for (NSString *photoUrl in dic[@"photos"]) {
            [self.country.photos addObject:[NSURL URLWithString:photoUrl]];
        }
        // 热门城市数组
        for (NSDictionary *cityDic in dic[@"hot_city"]) {
            CityModel *city = [[CityModel alloc]init];
            [city setValuesForKeysWithDictionary:cityDic];
            [self.arrayOfCity addObject:city];
            [city release];
        }
        [self.activityIndicatorView stopAnimating];
        [self createView];
        [_country release];
        
    }];
    
}

- (void)setLightDayModel
{
    [NightManager setBackgroundColorWithView:self.view];
    [NightManager setBackgroundColorWithCollectionView:self.collectionView];
    [NightManager setButtonTitleColorWithButton:self.moreCityButton];
}
- (void)setNightDayModel
{
    [NightManager setBackgroundColorWithView:self.view];
    [NightManager setBackgroundColorWithCollectionView:self.collectionView];
    [NightManager setButtonTitleColorWithButton:self.moreCityButton];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
